package ListDemo;

/**
 * package ListDemo;

/**
 * 分别通过2个线程，打印出1到100的数字
 */
public class ThreadDemo1 {
    public static void main(String[] args) {
        // 创建第一个线程，打印奇数
        PrintThread thread1 = new PrintThread(1, 2, 100);
        thread1.setName("Thread-奇数");

        // 创建第二个线程，打印偶数
        PrintThread thread2 = new PrintThread(2, 2, 100);
        thread2.setName("Thread-偶数");

        // 启动两个线程
        thread1.start();
        thread2.start();
    }

    static class PrintThread extends Thread {
        private int start;
        private int step;
        private int end;

        public PrintThread(int start, int step, int end) {
            this.start = start;
            this.step = step;
            this.end = end;
        }

        @Override
        public void run() {
            // 按照指定的步长打印数字
            for (int i = start; i <= end; i += step) {
                System.out.println(Thread.currentThread().getName() + ": " + i);
                try {
                    // 添加短暂延迟，便于观察输出顺序
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        }
    }
}




